<template>
    <el-dialog :visible.sync="visible" :title="dataForm.jobId ? '编辑' : '新增'" append-to-body>
        <el-form ref="dataForm" :model="dataForm" :rules="ruleValidate" label-width="120px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="学生学号" prop="studentNo">
                        <el-input @input="selectStudent" v-model="dataForm.studentNo" placeholder="输入获奖学生"
                            clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="学生姓名" prop="studentName">
                        <el-input v-model="dataForm.studentName" disabled clearable></el-input>
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="12">
                    <el-form-item label="创业类型" prop="activityType">
                        <el-select v-model="dataForm.activityType" clearable placeholder="请选择" style="width: 100%">
                            <el-option v-for="dict in dict.type.sys_activity_type" :key="dict.value"
                                :label="dict.label" :value="dict.value"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col> -->
                <el-col :span="12">
                    <el-form-item label="违纪类型" prop="violationCategory">
                        <el-select v-model="dataForm.violationCategory" clearable placeholder="输入违纪类型" style="width: 100%">
                            <el-option v-for="dict in dict.type.violation_category" :key="dict.value" :label="dict.label"
                                :value="dict.value"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="处分类型" prop="violationType">
                        <el-select v-model="dataForm.violationType" clearable placeholder="输入处分类型" style="width: 100%">
                            <el-option v-for="dict in dict.type.violation_type" :key="dict.value" :label="dict.label"
                                :value="dict.value"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="扣除分值" prop="realScore">
                        <el-input v-model="dataForm.realScore" type="number" clearable></el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="12">
                    <el-form-item label="处分说明" prop="violationDescribe">
                        <el-input type="textarea" v-model="dataForm.violationDescribe" placeholder="输入处分说明"
                            clearable></el-input>
                    </el-form-item>
                </el-col>

                <div v-if="dataForm.id">
                    <el-col :span="12">
                        <el-form-item label="违纪状态" prop="violationStatus">
                            <el-select v-model="dataForm.violationStatus" clearable placeholder="选择违纪状态"
                                style="width: 100%">
                                <el-option v-for="dict in dict.type.violation_status" :key="dict.value" :label="dict.label"
                                    :value="dict.value"></el-option>
                            </el-select>

                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="申诉情况" prop="represenStatus">
                            <el-select v-model="dataForm.represenStatus" clearable placeholder="选择申诉情况" style="width: 100%">
                                <el-option v-for="dict in dict.type.represen_status" :key="dict.value" :label="dict.label"
                                    :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="申诉内容" prop="violationDescribe">
                            <el-input type="textarea" v-model="dataForm.violationDescribe" placeholder="输入处分说明"
                                clearable></el-input>
                        </el-form-item>
                    </el-col>
                </div>

            </el-row>
        </el-form>
        <div slot="footer">
            <el-button type="text" @click="handleCancel">取消</el-button>
            <el-button type="primary" :loading="showLoading" @click="handleSubmit">保存</el-button>
        </div>
    </el-dialog>
</template>
<script>
import {
    queryStudent,
} from "@/api/information/studentInfor";
import { addData, editData } from "@/api/assessmentCenter/punish";
export default {
    data() {
        return {
            visible: false,
            showLoading: false,
            dataForm: {
                violationCategory: '',
                violationType: '',
                violationDescribe: '',
                studentNo: null,
                studentName: '',
                realScore: 0
            },
            ruleValidate: {
                violationCategory: [
                    { required: true, message: '请输入违纪类型', trigger: 'blur' }
                ],
                violationType: [
                    { required: true, message: '请输入处分类型', trigger: 'blur' }
                ],
                violationDescribe: [
                    { required: true, message: '请输入处分说明', trigger: 'blur' }
                ],
                studentNo: [
                    { required: true, message: '请输入违纪学生学号', trigger: 'blur' }
                ],
                realScore: [
                    { required: true, message: '请输入扣除分值', trigger: 'blur' }
                ],

            }
        }
    },
    dicts: ["violation_category", "violation_type","violation_status","represen_status"],
    methods: {
        init(data) {
            // add or update
            this.visible = true;
            this.$nextTick(() => {
                this.$refs['dataForm']?.resetFields();
            });
            if (data) {
                this.$nextTick(() => {
                    this.dataForm = {...data};
                    this.dataForm.studentNo = data.registerCode;
                    this.dataForm.represenStatus+=''
                    this.dataForm.violationStatus+=''
                    
                });
            } else {
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields();
                });
            }
        },

        handleCancel() {
            this.visible = false;
            this.disablePassword = true;
            this.showLoading = false;
        },
        handleSubmit() { // 表单提交
            let param = { ...this.dataForm }
            this.$refs['dataForm'].validate(async (valid) => {
                if (valid) {
                    if (!this.dataForm.studentName) {
                        this.$message.error("请输入正确的学号")
                        return;
                    }
                    this.showLoading = true;
                    let data = {}
                    if (this.dataForm.id) {
                        data = await editData(param)
                    } else {
                        data = await addData(param)
                    }
                    if (data && data.code === 200) {
                        this.$modal.msgSuccess("操作成功");
                        this.handleCancel();
                        this.$emit('refreshDataList')

                    } else {
                        this.$message.error(data.msg);
                        this.showLoading = false;
                    }
                }
            })
        },
        // 查询学生信息
        async selectStudent() {
            const { data } = await queryStudent({
                registerOrSno: this.dataForm.studentNo
            })
            if (data) {
                this.dataForm.studentName = data.name
                this.dataForm.studentId = data.id
            } else {
                this.dataForm.studentName = ''
                this.dataForm.studentId = null;
            }
        }
    }
}
</script>
  